package net.bplaced.streetfighter.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * @author Sebastian Hoffmann <bo.hoffi@googlemail.com>
 */
public abstract class AbstractAdapter<T>
        extends BaseAdapter {

    protected final Context context;
    protected final List<T> items;

    protected AbstractAdapter(final Context context, final List<T> items) {

        this.context = context;
        this.items = items;
    }

    public void add(T item) {

        this.items.add(item);
        this.notifyDataSetChanged();
    }

    public void addAll(List<T> items) {

        this.items.addAll(items);
        this.notifyDataSetChanged();
    }

    public void addAll(T[] items) {

        for (final T i : items) {
            this.items.add(i);
        }
        this.notifyDataSetChanged();
    }

    public void clear() {

        this.items.clear();
        this.notifyDataSetChanged();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {

        return this.items.size();
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public T getItem(int position) {

        return this.items.get(position);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {

        return position;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        return this.getView(position, convertView,
                (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
    }

    abstract View getView(int position, View convertView, LayoutInflater inflater);
}
